/**
 * @author: 團長
 * @date: 2013-5-15-上午11:20:05
 */
package net.loyintean.pearlshell.sm.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import net.loyintean.pearlshell.basedb.dao.BaseDao;
import net.loyintean.pearlshell.sm.model.BaseMybatisDto;

/**
 * BaseDao接口的一个实现类，与mybatis耦合
 * <p>
 * 2013年09月18日 团长 增加IBatchDao接口
 * <p>
 * 2014-2-24 Loy: set default statement id for BaseDao's method, and delete some
 * outdate code. v2.1.0
 * 
 * @author 團長
 * @since 2014-2-24
 * @version 2.1.0
 */
public class MybatisBaseDaoImpl<T extends BaseMybatisDto> extends
		SqlSessionDaoSupport implements BaseDao<T> {

	/**
	 * 2014-2-24 Loy set the statement id as dto.getClass().getName() +
	 * ".insertSingleDto" by default.
	 * 
	 * @author 團長
	 * @param dto
	 *            if the field saveSingleDto is blank, set the default value.
	 * @see net.loyintean.evaluation.base.dao.BaseDao#insertSingleDto(net.loyintean.evaluation.base.model.BaseDto,
	 *      java.lang.String)
	 * 
	 */
	@Override
	public int insertSingleDto(T dto) throws SQLException {

		if (StringUtils.isBlank(dto.getSaveSingleDtoSql())) {
			dto.setSaveSingleDtoSql(dto.getClass().getName()
					+ ".insertSingleDto");
		}

		int result = getSqlSession().insert(dto.getSaveSingleDtoSql(), dto);

		return result;
	}

	/**
	 * 2014-2-24 Loy set the statement id as dto.getClass().getName() +
	 * ".deleteSingleDto" by default.
	 * 
	 * @author 團長
	 * @param dto
	 *            if the field saveSingleDto is blank, set the default value.
	 * @see net.loyintean.evaluation.base.dao.BaseDao#deleteSingleDto(net.loyintean.evaluation.base.model.BaseDto,
	 *      java.lang.String)
	 * 
	 */
	@Override
	public int deleteSingleDto(T dto) throws SQLException {

		if (StringUtils.isBlank(dto.getRemoveSingleDtoSql())) {
			dto.setRemoveSingleDtoSql(dto.getClass().getName()
					+ ".deleteSingleDto");
		}

		int rows = getSqlSession().delete(dto.getRemoveSingleDtoSql(), dto);
		return rows;
	}

	/**
	 * 2014-2-24 Loy set the statement id as dto.getClass().getName() +
	 * ".updateSingleDto" by default.
	 * 
	 * @author 團長
	 * @param dto
	 *            if the field saveSingleDto is blank, set the default value.
	 * @see net.loyintean.evaluation.base.dao.BaseDao#updateSingleDto(net.loyintean.evaluation.base.model.BaseDto,
	 *      java.lang.String)
	 * 
	 */
	@Override
	public int updateSingleDto(T dto) throws SQLException {

		if (StringUtils.isBlank(dto.getEditSingleDtoSql())) {
			dto.setEditSingleDtoSql(dto.getClass().getName()
					+ ".updateSingleDto");
		}

		int rows = getSqlSession().update(dto.getEditSingleDtoSql(), dto);
		return rows;
	}

	/**
	 * 2014-2-24 Loy set the statement id as dto.getClass().getName() +
	 * ".selectSingleDto" by default.
	 * 
	 * @author 團長
	 * @param dto
	 *            if the field saveSingleDto is blank, set the default value.
	 * @see net.loyintean.evaluation.base.dao.BaseDao#selectSingleDto(net.loyintean.evaluation.base.model.BaseDto,
	 *      java.lang.String)
	 * 
	 */
	@Override
	public T selectSingleDto(T dto) throws SQLException {

		if (StringUtils.isBlank(dto.getQuerySingleDtoSql())) {
			dto.setQuerySingleDtoSql(dto.getClass().getName()
					+ ".selectSingleDto");
		}

		@SuppressWarnings("unchecked")
		T result = (T) getSqlSession().selectOne(dto.getQuerySingleDtoSql(),
				dto);

		return result;
	}

	/**
	 * 2014-2-24 Loy set the statement id as dto.getClass().getName() +
	 * ".selectDtoList" by default.
	 * 
	 * @author 團長
	 * @param dto
	 *            if the field saveSingleDto is blank, set the default value.
	 * @see net.loyintean.evaluation.base.dao.BaseDao#selectDtoList(net.loyintean.evaluation.base.model.BaseDto,
	 *      java.lang.String)
	 * 
	 */
	@Override
	public List<T> selectDtoList(T dto) throws SQLException {

		if (StringUtils.isBlank(dto.getQueryDtoListSql())) {
			dto.setQueryDtoListSql(dto.getClass().getName() + ".selectDtoList");
		}

		List<T> result = getSqlSession().selectList(dto.getQueryDtoListSql(),
				dto);
		return result;
	}

	/**
	 * 2014-2-24 Loy set the statement id as dto.getClass().getName() +
	 * ".selectPagedList" by default.
	 * 
	 * @author 團長
	 * @param dto
	 *            if the field saveSingleDto is blank, set the default value.
	 * @see net.loyintean.evaluation.base.dao.BaseDao#selectPagedList(net.loyintean.evaluation.base.model.BaseDto,
	 *      java.lang.String)
	 * 
	 */
	@Override
	public List<T> selectPagedList(T dto) throws SQLException {

		if (StringUtils.isBlank(dto.getQueryPagedListSql())) {
			dto.setQueryPagedListSql(dto.getClass().getName()
					+ ".selectPagedList");
		}

		List<T> paged = getSqlSession().selectList(dto.getQueryPagedListSql(),
				dto);
		return paged;
	}

	/**
	 * 2014-2-24 Loy set the statement id as dto.getClass().getName() +
	 * ".selectTotalCount" by default.
	 * 
	 * @author 團長
	 * @param dto
	 *            if the field saveSingleDto is blank, set the default value.
	 * @see net.loyintean.evaluation.base.dao.BaseDao#selectTotalCount(net.loyintean.evaluation.base.model.BaseDto,
	 *      java.lang.String)
	 * 
	 */
	@Override
	public int selectTotalCount(T dto) throws SQLException {

		if (StringUtils.isBlank(dto.getQueryTotalCountSql())) {
			dto.setQueryTotalCountSql(dto.getClass().getName()
					+ ".selectTotalCount");
		}
		int count = (Integer) getSqlSession().selectOne(
				dto.getQueryTotalCountSql(), dto);
		return count;
	}
}
